﻿using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using System.IO;

namespace NetCoreMvcTest.Utils
{
    /// <summary>
    /// 路径帮助类
    /// </summary>
    public static class PathUtil
    {
        /// <summary>
        /// 获取wwwroot路径
        /// </summary>
        /// <returns></returns>
        public static string WebRootPath
        {
            get
            {
                var env = ServiceLocator.Instance.GetService<IWebHostEnvironment>();
                return env != null ? env.WebRootPath : "";
            }
        }

        /// <summary>
        /// 获取根路径 或者 bin路径
        /// </summary>
        /// <returns></returns>
        public static string ContentRootPath
        {
            get
            {
                var env = ServiceLocator.Instance.GetService<IWebHostEnvironment>();
                if (env != null)
                {
                    return env.ContentRootPath;
                }
                else
                {
                    var path = AppDomain.CurrentDomain.BaseDirectory;
                    if (path.EndsWith(Path.DirectorySeparatorChar))
                    {
                        path = path.TrimEnd(Path.DirectorySeparatorChar);
                    }
                    return path;
                }
            }
        }
    }
}
